package com.leyou.search.mq;

import com.leyou.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.leyou.common.constants.MQConstants.Exchange.ITEM_EXCHANGE_NAME;
import static com.leyou.common.constants.MQConstants.Queue.SEARCH_ITEM_DOWN;
import static com.leyou.common.constants.MQConstants.Queue.SEARCH_ITEM_UP;
import static com.leyou.common.constants.MQConstants.RoutingKey.ITEM_DOWN_KEY;
import static com.leyou.common.constants.MQConstants.RoutingKey.ITEM_UP_KEY;

@Component
public class ItemListener {

    @Autowired
    private SearchService searchService;

    //商品上架的时候：这个方法创建索引
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name=SEARCH_ITEM_UP , durable = "true"), // 配置了队列的名称和持久化
            exchange = @Exchange(
                    name = ITEM_EXCHANGE_NAME,   // 交换机名称
                    type = ExchangeTypes.TOPIC  //  类型
            ),
            key = ITEM_UP_KEY
    ))
    public void itemUp(Long id){
        searchService.createItemIndex(id);
    }

    //商品下架的时候：这个方法删除索引
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name=SEARCH_ITEM_DOWN , durable = "true"), // 配置了队列的名称和持久化
            exchange = @Exchange(
                    name = ITEM_EXCHANGE_NAME,   // 交换机名称
                    type = ExchangeTypes.TOPIC  //  类型
            ),
            key = ITEM_DOWN_KEY
    ))
    public void itemDown(Long id){
        searchService.deleteItemIndex(id);
    }

}
